added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSASPNETBackgroundWorker / Global.asax.cs
blob2a38d987905a6d3a2b050ff075bd31427bc962b8
1 /****************************** Module Header ******************************\
2 * Module Name: Global.asax.cs
3 * Project: CSASPNETBackgroundWorker
4 * Copyright (c) Microsoft Corporation
6 * When application starts up, Application_Start() method will be called.
7 * In the Application_Start() method, it creates a BackgroundWorker object and
8 * then stores it in Application State. Therefore, the worker_DoWork() will
9 * keep executing until application ends.
11 * This source is subject to the Microsoft Public License.
12 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 * All other rights reserved.
15 \*****************************************************************************/
17 using System;
18 using System.Threading;
20 namespace CSASPNETBackgroundWorker
22 public class Global : System.Web.HttpApplication
24 /// <summary>
25 /// Create a Background Worker to run the operation
26 /// whenever the application start.
27 /// </summary>
28 protected void Application_Start(object sender, EventArgs e)
30 BackgroundWorker worker = new BackgroundWorker();
31 worker.DoWork += new BackgroundWorker.DoWorkEventHandler(worker_DoWork);
32 worker.RunWorker(null);
34 // This Background Worker is Applicatoin Level,
35 // so it will keep working and it is shared by all users.
36 Application["worker"] = worker;
39 /// <summary>
40 /// This operation will work without the end.
41 /// </summary>
42 void worker_DoWork(ref int progress,
43 ref object _result, params object[] arguments)
45 // Do the operation every 1 second wihout the end.
46 while (true)
48 Thread.Sleep(1000);
50 // This statement will run every 1 second.
51 progress++;
53 // Other logic which you want it to keep running.
54 // You can do some scheduled tasks here by checking DateTime.Now.